<template>


    <el-button type="primary"  @click="handleAdd">
       新增会员商品
      </el-button>
    <el-table :data="tableData" style="width: 100%">
        <el-table-column label="商品标题" prop="vipTitle" />
        <el-table-column label="商品类型" prop="type" />
        <el-table-column label="商品金额" prop="totalFee" />
        <el-table-column label="商品描述" prop="description" />
        <el-table-column label="创建时间" prop="createTime" />
        <el-table-column align="right">
          <template #header>
        <el-input v-model="queryForm.query"  @input="initUserList" placeholder="请输入内容"></el-input>
          </template>
          <template #default="scope">
            <el-button size="small"  type="primary" @click="handleEdit(scope.$index, scope.row)">
              编辑
            </el-button>
        
            <el-button size="small"  type="primary" @click="deleteRole(scope.row.roleId)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          v-model:current-page="queryForm.pageNum"
          v-model:page-size="queryForm.pageSize"
          :page-sizes="[10, 20, 30, 40]"
          :size="size"
    
          background layout="prev, pager, next"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"/>
    
        <el-dialog v-model="dialogFormVisible" title="新增会员商品" width="500">
          <el-form :model="goodsFrom" :rules="rules" ref="formRef" label-width="auto" style="max-width: 600px">
      
        <el-form-item prop="vipTitle" label="商品标题">
          <el-input v-model="goodsFrom.vipTitle" placeholder="请输入商品标题"></el-input>
        </el-form-item>

                    <el-form-item prop="type" label="选择类别">
                    <el-select
                      v-model="goodsFrom.type"
                      placeholder="Select"
                      size="large"
                      style="width: 240px">
                      <el-option
                        v-for="item in goodsTypeList"
                        :key="item.dictValue"
                        :label="item.dictLabel"
                        :value="item.dictValue"
                      />
                    </el-select>
                  </el-form-item>
             <el-form-item label="商品金额" prop="totalFee" >
            <div style="margin: 20px 0" />
            <el-input v-model="goodsFrom.totalFee"  placeholder="请输入商品金额"/>
          </el-form-item>
        
          <el-form-item label="商品描述" prop="description" >
            <div style="margin: 20px 0" />
            <el-input v-model="goodsFrom.description"  placeholder="请输入商品描述"/>
          </el-form-item>
       
      </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="resetForm">取消</el-button>
            <el-button type="primary" @click=" submitForm()">
              提交
            </el-button>
          </div>
        </template>
      </el-dialog>

      
    </template>
    
    <script  setup>
         import {ref,reactive} from 'vue';
         import request from '@/util/request';
         import { ElMessage ,ElMessageBox} from 'element-plus'
         const dialogFormVisible = ref(false)
         const tableData=ref([]);
  
         const goodsTypeList=ref([]);
         const total=ref(0);
         const queryForm=ref({
            query:"",
            pageNum:1,
            pageSize:10
         })
         const initUserList=async ()=>{
           const res= await request.post("/vip/goods/list",queryForm.value)
           tableData.value=res.data.data.dataList;
           total.value=res.data.data.total;
         }
         initUserList();

         const iniseekTypeList=async ()=>{
           const res= await request.get("dict/getListByType/vipgoods_type")
           goodsTypeList.value=res.data.data;
           
         }
         iniseekTypeList();

         const handleSizeChange=(pageSize)=>{
            queryForm.value.pageNum=1;
            queryForm.value.pageSize=pageSize
            initUserList();
         }
         const handleCurrentChange=(pageNum)=>{
            queryForm.value.pageNum=pageNum;
            initUserList();
         }

     const goodsFrom = reactive({
            id:'',
            description:'',
            type:'',
            vipTitle:'',
            totalFee:''
    })
    
    const formRef = ref(null);
    const rules = {
      vipTitle: [
        { required: true, message: '请输入商品标题', trigger: 'blur' }
      ],
      type: [
        { required: true, message: '请选择商品类型', trigger: 'blur' }
      ],
      totalFee: [
        { required: true, message: '请输入商品金额', trigger: 'blur' }
      ],
      description: [
        { required: true, message: '请输入商品信息', trigger: 'blur' }
      ]
    };

    const deleteRole=(id)=>{
  ElMessageBox.confirm(
          '确认删除',
          'Warning',
          {
            confirmButtonText: 'OK',
            cancelButtonText: 'Cancel',
            type: 'warning',
          }
      ).then(() => {
          request.get("role/"+id).then(res=>{
            if(res.data.code==200){
              initUserList()
              ElMessage.success("删除成功")
            }else{
              ElMessage.error(res.data.msg);
            }
          })
    })}
     
    const submitForm = () => {
      formRef.value.validate((valid) => {
        if (valid) {
          request.post("vip/goods/saveOrUpdate",goodsFrom).then(res=>{
           if(res.data.code==200){
            initUserList();
            dialogFormVisible.value = false
            ElMessage.success("新增成功")
           }else{
            ElMessage.error(res.data.msg);
           }
          })
        } else {
          return false;
        }
      });
    };
    const handleEdit=(index,row)=>{
     let obj=  Object.assign({}, row)
     goodsFrom.id=obj.id;
     goodsFrom.description=obj.description;
     goodsFrom.type=obj.type;
     goodsFrom.vipTitle=obj.vipTitle;
     goodsFrom.totalFee=obj.totalFee;
    dialogFormVisible.value = true
    }



    
    const handleAdd=()=>{
      dialogFormVisible.value = true
    }
    
    const resetForm = () => {
      goodsFrom.id='';
     goodsFrom.description='';
     goodsFrom.type='';
     goodsFrom.vipTitle='';
     goodsFrom.totalFee='';
          dialogFormVisible.value = false
        }

        





    </script>
    
    <style lang="scss" scoped>
    
    </style>
    